﻿//********************************************
//          Nội dung: xu ly man hinh quan ly kho            *
//          Stored: SP_mh_quanlykho                           *
//          Ngươi thực hiện: To Hoai Chuong                 *
//********************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using LPV_POS.Baocao;
using Control;
using LPV_POS.mh_pos.mh_khac;
using LPV_POS.Baocao.Ellic;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos
{
    public partial class mh_quanlykho : DevExpress.XtraEditors.XtraForm
    {
        Mod_messagebox mess;
        thuvien tv;
        string[] s;
        object[] o;
        DataTable tb;
        DataTable tb_hhlienket;
        DataTable tb_dinhmuchh;
        DataTable tb_dinhmuchh_temp;
        DataTable tb_nhapxuatkho;
        DataSet dset;
        private string str_makho;
        private Boolean them_phieu_nguyenlieu = false;
        private Boolean them_phieu_nguyenlieu_ct = false;
        private string str_tennguyenlieu = "";
        private int str_manguyenlieu;
        private string str_tendvt = "";
        private string str_madvt = "";
        float m_soluongbandau = 0;
        CultureInfo culture;
        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                this.tab_quanlykho.Text = rm.GetString("khohanghoa", culture);
                this.page_khonguyenlieu.Text = rm.GetString("khonguyenlieu", culture);
                this.grid_khohang.GroupPanelText = rm.GetString("danhsachkhohang", culture);
                label1.Text = rm.GetString("cauhinh", culture);
                label2.Text = rm.GetString("danhsach", culture);
                check_allowtoithieu.Text = rm.GetString("banduoimin", culture);
                check_allowbanamkho.Text = rm.GetString("banamkho", culture);
                check_warntoithieu.Text = rm.GetString("canhbaobanduoimin", culture);
                this.col_ten.Caption = rm.GetString("ten", culture);
                this.col_tonkho.Caption = rm.GetString("tonkho", culture);
                groupControl1.Text = rm.GetString("thongtinkhohang", culture);
                check_usekho.Text = rm.GetString("sudungkho", culture);
                label5.Text = rm.GetString("tonkhotoida", culture);
                label3.Text = rm.GetString("tonkhotoithieu", culture);
                btnGia_NhapXuat.Text = rm.GetString("gia", culture);
                label4.Text = rm.GetString("soluong", culture);
                bton_nhap.Text = rm.GetString("nhap", culture);
                bton_xuat.Text = rm.GetString("xuat", culture);
                label16.Text = rm.GetString("barcode", culture);
                bton_chon.Text = rm.GetString("chonnhomchu", culture);
                bton_tonkhobandau.Text = rm.GetString("tonkhobandau", culture);
                bton_luu.Text = rm.GetString("luu", culture);
                bton_thoat.Text = rm.GetString("thoat", culture);
                bton_use_barcode.Text = rm.GetString("sudungbarcode", culture);


                groupBox1.Text = rm.GetString("phieunhapnguyenlieu", culture);
                label8.Text = rm.GetString("khohang", culture);
                //sophieu.Text = rm.GetString("khongvat", culture);
                //ngay.Text = rm.GetString("khongvat", culture);
                label14.Text = rm.GetString("tongtien", culture);
                label15.Text = rm.GetString("VNdong", culture);
                bton_them_phieu_nl.Text = rm.GetString("them", culture);
                bton_sua_nl.Text = rm.GetString("sua", culture);
                bton_xoa_nl.Text = rm.GetString("xoa", culture);
                bton_luu_nl.Text = rm.GetString("luu", culture);
                this.col_nguyenlieu_ngaylapphieu.Caption = rm.GetString("ngay", culture);
                this.col_nguyenlieu_sophieu.Caption= rm.GetString("sophieu", culture);
                this.col_tongtien.Caption= rm.GetString("tongtien", culture);
                group_chitietphieu.Text = rm.GetString("chitietphieu", culture);
                label21.Text = rm.GetString("tennguyenlieu", culture);
                label10.Text = rm.GetString("tonkhotoida", culture);
                label9.Text = rm.GetString("tonkhotoithieu", culture);
                label7.Text = rm.GetString("soluong", culture);
                label13.Text = rm.GetString("thanhtien", culture);
                bton_them_ctnl.Text = rm.GetString("them", culture);
                bton_sua_ctnl.Text = rm.GetString("sua", culture);
                bton_xoa_ctnl.Text = rm.GetString("xoa", culture);
                bton_luu_ctnl.Text = rm.GetString("luu", culture);
                bton_thoat_nguyenlieu.Text = rm.GetString("thoat", culture);
                col_ten_nguyenlieu.Caption = rm.GetString("nguyenlieu", culture);
                col_soluong_nguyenlieu.Caption = rm.GetString("soluong", culture);
                col_nl_tendvt.Caption = rm.GetString("dvt", culture);
                col_thanhtien.Caption = rm.GetString("thanhtien", culture);
                label12.Text = rm.GetString("VNdong", culture);
                label11.Text = rm.GetString("sophieu", culture);
                label6.Text = rm.GetString("ngay", culture);
                //bton_sua_nl.Text = rm.GetString("sua", culture);
                //bton_sua_nl.Text = rm.GetString("sua", culture);
                //bton_sua_nl.Text = rm.GetString("sua", culture);

                //bton_sua_nl.Text = rm.GetString("sua", culture);





            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        #region  Cac ham va phuong thuc
        private DataTable f_dskhohang()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("SP_mh_quanlykho", s, o);
            return tb;
        }

        //private DataTable f_dsnhomhh()
        //{
        //    tv = new thuvien();
        //     s = new string[] { "@flag", "@choose","@manhomhang" };
        //    o = new object[] { 1, 1,m_mahh};
        //    tb = tv.SP_SelectDataTable("SP_frmquanlykho", s, o);
        //    return tb;
        //}

        private DataTable f_luoihhlienket(string mahh)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@mahh" };
            o = new object[] { 1, 4, mahh };
            tb = tv.SP_SelectDataTable("SP_mh_quanlykho", s, o);
            return tb;
        }
        private DataTable f_dinhmuctonkho()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 5 };
            tb = tv.SP_SelectDataTable("SP_mh_quanlykho", s, o);
            return tb;
        }

        private DataTable f_createtable_lienket()
        {
            DataTable tb_hh = new DataTable();
            tb_hh.Columns.Add("MAHH", typeof(string));
            tb_hh.Columns.Add("MAHHLIENKET", typeof(string));
            tb_hh.Columns.Add("SOLUONG", typeof(float));
            tb_hh.Columns.Add("GHICHU", typeof(string));
            tb_hh.Columns.Add("TENHHLIENKET", typeof(string));

            return tb_hh;
        }

        private DataTable f_createtable_dinhmuctoithieu()
        {
            DataTable tb_hh = new DataTable();
            tb_hh.Columns.Add("MAKHO", typeof(string));
            tb_hh.Columns.Add("MAHH", typeof(string));
            tb_hh.Columns.Add("SOLUONG_MIN", typeof(decimal));
            tb_hh.Columns.Add("SOLUONG_MAX", typeof(decimal));
            return tb_hh;
        }

        private DataTable f_createtable_nhapxuatkho()
        {
            DataTable tb_hh = new DataTable();
            tb_hh.Columns.Add("MAKHO", typeof(string));
            tb_hh.Columns.Add("MAHH", typeof(string));
            tb_hh.Columns.Add("SOLUONG_NHAP", typeof(decimal));
            tb_hh.Columns.Add("SOLUONG_XUAT", typeof(decimal));
            tb_hh.Columns.Add("GIAMUA", typeof(decimal));
            tb_hh.Columns.Add("GIABAN", typeof(decimal));
            return tb_hh;
        }
        // luu dinh mức tồn kho tối thiểu vào cơ sở dữ liệu
        private Int16 f_luudinhmuctonkho()
        {
            try
            {
                if (tb_dinhmuchh_temp.Rows.Count > 0)
                {
                    for (int i = 0; i < tb_dinhmuchh_temp.Rows.Count; i++)
                    {
                        string makho = tb_dinhmuchh_temp.Rows[i]["MAKHO"].ToString();
                        string mahh = tb_dinhmuchh_temp.Rows[i]["MAHH"].ToString();
                        decimal soluong = Convert.ToDecimal(tb_dinhmuchh_temp.Rows[i]["SOLUONG_MIN"].ToString());
                        decimal soluong_max = Convert.ToDecimal(tb_dinhmuchh_temp.Rows[i]["SOLUONG_MAX"].ToString());

                        s = new string[] { "@flag", "@choose", "@makho", "@mahh", "@soluong", "@soluong_max" };
                        o = new object[] { 2, 2, makho, mahh, soluong, soluong_max };
                        tv = new thuvien();
                        tv.SP_Update("sp_mh_quanlykho", s, o);

                    }
                }

                return 1;

            }
            catch (Exception)
            {

                return 0;
            }

        }

        private Int16 f_luuxuatnhapkho()
        {
            try
            {
                if (tb_nhapxuatkho.Rows.Count > 0)
                {
                    for (int i = 0; i < tb_nhapxuatkho.Rows.Count; i++)
                    {
                        string makho = tb_nhapxuatkho.Rows[i]["MAKHO"]+"";
                        string mahh = tb_nhapxuatkho.Rows[i]["MAHH"]+"";
                        decimal sl_nhap = 0;
                        decimal.TryParse(tb_nhapxuatkho.Rows[i]["SOLUONG_NHAP"] + "", out sl_nhap);

                        decimal sl_xuat = 0;
                        decimal.TryParse(tb_nhapxuatkho.Rows[i]["SOLUONG_XUAT"] + "", out sl_xuat);

                        decimal gia_mua = 0;
                        decimal.TryParse(tb_nhapxuatkho.Rows[i]["GIAMUA"] + "", out gia_mua);

                        decimal gia_ban = 0;
                        decimal.TryParse(tb_nhapxuatkho.Rows[i]["GIABAN"] + "", out gia_ban);


                        if (sl_nhap > 0)
                        {
                            s = new string[] { "@flag", "@choose", "@makho", "@mahh", "@sl_nhap", "@sl_xuat", "@gia_mua", "@gia_ban" };
                            o = new object[] { 2, 3, makho, mahh, sl_nhap, sl_xuat,gia_mua,gia_ban };
                            tv = new thuvien();
                            tv.SP_Update("sp_mh_quanlykho", s, o);
                        }
                        if (sl_xuat > 0)
                        {
                            s = new string[] { "@flag", "@choose", "@makho", "@mahh", "@sl_nhap", "@sl_xuat", "@gia_mua", "@gia_ban" };
                            o = new object[] { 2, 4, makho, mahh, sl_nhap, sl_xuat, gia_mua, gia_ban };
                            tv = new thuvien();
                            tv.SP_Update("sp_mh_quanlykho", s, o);
                        }
                    }
                }

                return 1;

            }
            catch (Exception)
            {

                return 0;
            }
        }


        // luu cho phep ban hang am kho
        private Int16 f_luu_cpbanamkho()
        {
            try
            {
                if (check_allowbanamkho.Checked == true)
                {
                    s = new string[] { "@flag", "@choose", "@trangthai" };
                    o = new object[] { 3, 1, 1 };
                    tv = new thuvien();
                    tv.SP_Update("sp_mh_quanlykho", s, o);
                }

                else if (check_allowbanamkho.Checked == false)
                {
                    s = new string[] { "@flag", "@choose", "@trangthai" };
                    o = new object[] { 3, 1, 0 };
                    tv = new thuvien();
                    tv.SP_Update("sp_mh_quanlykho", s, o);
                }
                return 1;

            }
            catch (Exception)
            {
                return 0;
            }
        }

        // luu cho phep ban hang duoi dinh muc toi thieu
        private Int16 f_luu_cpbhduoidmtt()
        {
            try
            {
                if (check_allowtoithieu.Checked == true)
                {
                    s = new string[] { "@flag", "@choose", "@trangthai" };
                    o = new object[] { 3, 2, 1 };
                    tv = new thuvien();
                    tv.SP_Update("sp_mh_quanlykho", s, o);
                }

                else if (check_allowtoithieu.Checked == false)
                {
                    s = new string[] { "@flag", "@choose", "@trangthai" };
                    o = new object[] { 3, 2, 0 };
                    tv = new thuvien();
                    tv.SP_Update("sp_mh_quanlykho", s, o);
                }
                return 1;

            }
            catch (Exception)
            {
                return 0;
            }
        }

        // canh bao hang hoa duoi dinh muc toi thieu

        private Int16 f_luu_cbhhduoidmtt()
        {
            try
            {
                if (check_warntoithieu.Checked == true)
                {
                    s = new string[] { "@flag", "@choose", "@trangthai" };
                    o = new object[] { 3, 3, 1 };
                    tv = new thuvien();
                    tv.SP_Update("sp_mh_quanlykho", s, o);
                }

                else if (check_warntoithieu.Checked == false)
                {
                    s = new string[] { "@flag", "@choose", "@trangthai" };
                    o = new object[] { 3, 3, 0 };
                    tv = new thuvien();
                    tv.SP_Update("sp_mh_quanlykho", s, o);
                }
                return 1;

            }
            catch (Exception)
            {
                return 0;
            }
        }
        private DataTable f_selecttable_canhbao()
        {
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 6 };
            tv = new thuvien();
            tb = tv.SP_SelectDataTable("sp_mh_quanlykho", s, o);
            return tb;
        }

        private void f_showhh_barcode(string barcode)
        {
            s = new string[] { "@flag", "@choose", "@barcode" };
            o = new object[] { 1, 7, barcode };
            tb = tv.SP_SelectDataTable("sp_mh_quanlykho", s, o);
            luoi_hh.DataSource = tb;
            txt_barcode.Focus();
        }
        // asdfg


        #region " Quan ly kho nguyen lieu"

        private DataSet f_select_nguyenlieu(int flag, int chooose, string maphieunhapnguyenlieu)
        {
            tv = new thuvien();
            DataSet ds = tv.SP_SelectDataset("sp_kho_nguyenlieu", new string[] { "@flag", "@choose", "@id" }, new object[] { flag, chooose, maphieunhapnguyenlieu });
            return ds;
        }

        private int f_update_phieunhapnguyenlieu(int flag, int choose, string maphieunhapnguyenlieu, string makho, string ngay, decimal tongtien)
        {
            int ketqua = -1;
            ketqua = tv.SP_Update("sp_kho_nguyenlieu", new string[] { "@flag", "@choose", "@id", "@makho", "@ngay", "@tongtien" }, new object[] { flag, choose, maphieunhapnguyenlieu, makho, ngay, tongtien });
            return ketqua;
        }

        #endregion


        // hien thi ds kho nguyen lieu len combobox
        private void f_showdataoncombox(AXCombobox cb, DataTable tb, string value, string display)
        {
            cb.Properties.Items.Clear();
            cb.Datasource = null;
            cb.Datasource = tb;
            cb.DisplayField = display;
            cb.ValueFiled = value;
            cb._Init();
        }

        #endregion

        public mh_quanlykho()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            dset = f_select_nguyenlieu(1, 1, "");
            f_showdataoncombox(com_nguyenlieu_khohang, dset.Tables[1], "OBJID", "OBJNAME");
            
        }

        #region cac su kien

        private void mh_quanlykho_Load(object sender, EventArgs e)
        {

            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

            timer1.Enabled = true;
            timer1.Interval = 500;

            this.luoi_khohang.DataSource = f_dskhohang();
            tb_hhlienket = f_createtable_lienket();
            tb_dinhmuchh = f_createtable_dinhmuctoithieu();
            tb_dinhmuchh_temp = f_createtable_dinhmuctoithieu();
            tb_dinhmuchh = f_dinhmuctonkho();
            tb_nhapxuatkho = f_createtable_nhapxuatkho();
            if (check_allowtoithieu.Checked == false)
            {
                txt_toithieu.Enabled = false;
                txt_toida.Enabled = false;
                txt_nhapxuat.Enabled = false;
                txtGia_NhapXuat.Enabled = false;
                btnGia_NhapXuat.Enabled = false;
                bton_toithieu.Enabled = false;
                bton_toida.Enabled = false;
                bton_nhap.Enabled = false;
                //bton_xuat.Enabled = false;
            }

            if (check_hhlienket.Checked == false)
                luoi_hhlienket.Enabled = false;

            tb = null;
            tb = f_selecttable_canhbao();
            if (tb.Rows[0]["CPBANAMKHO"].ToString() == "True")
                check_allowbanamkho.Checked = true;
            else
                check_allowbanamkho.Checked = false;
            if (tb.Rows[0]["CPBANDUOIDMTT"].ToString() == "True")
                check_allowtoithieu.Checked = true;
            else
                check_allowtoithieu.Checked = false;
            if (tb.Rows[0]["CBDUOIDMTT"].ToString() == "True")
                check_warntoithieu.Checked = true;
            else
                check_warntoithieu.Checked = false;

            CancelButton = bton_thoat;

            #region "Quan ly nguyen lieu"

            date_nguyenlieu_ngaylapphieu.Text = DateTime.Today.ToString("dd/MM/yyyy");
            luoi_phieunhapnguyenlieu.DataSource = dset.Tables[0];
            txt_nhapxuat.Text = "1";
            txt_barcode.Focus();
            check_usekho.Checked = true;

            #endregion
        }

        private void gridView_khohang_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            //   str_makho = grid.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_khohang.FocusedColumn;
            str_makho = grid_khohang.GetRowCellDisplayText(chiso, col_makho) + "";
        }

        private void check_usekho_CheckedChanged(object sender, EventArgs e)
        {

            if (luoi_hh.DefaultView.RowCount <= 0 || luoi_khohang.DefaultView.RowCount <= 0)
            {
                check_usekho.Checked = false;
                txt_barcode.Focus();
                return;
            }
            if (check_usekho.Checked == true)
            {
                txt_nhapxuat.Enabled = true;
                txt_toida.Enabled = true;
                txt_toithieu.Enabled = true;
                txtGia_NhapXuat.Enabled = true;
                btnGia_NhapXuat.Enabled = true;
                bton_toida.Enabled = true;
                bton_nhap.Enabled = true;
                //bton_xuat.Enabled = true;
                bton_toithieu.Enabled = true;
                txt_barcode.Focus();
            }
            else
            {
                txt_nhapxuat.Enabled = false;
                txt_toida.Enabled = false;
                txt_toithieu.Enabled = false;
                txtGia_NhapXuat.Enabled = false;
                btnGia_NhapXuat.Enabled = false;
                bton_toida.Enabled = false;
                bton_nhap.Enabled = false;
                //bton_xuat.Enabled = false;
                bton_toithieu.Enabled = false;
                txt_barcode.Focus();
            }
        }

        private void check_hhlienket_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                // neu chua chon hàng hoa de lien ket thi khong cho chon checkbox nay
                if (luoi_hh.DefaultView.RowCount <= 0)
                {
                    check_hhlienket.Checked = false;
                    return;
                }
                if (check_hhlienket.Checked == true)
                {
                    luoi_hhlienket.Enabled = true;
                    m_mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
                    luoi_hhlienket.DataSource = null;
                    luoi_hhlienket.DataSource = f_luoihhlienket(m_mahh);
                    tb_hhlienket.PrimaryKey = new DataColumn[] { tb_hhlienket.Columns["MAHH"], tb_hhlienket.Columns["MAHHLIENKET"] };
                    tb_hhlienket = f_luoihhlienket(m_mahh);

                    m_mahh = "";
                }
                else
                {
                    luoi_hhlienket.Enabled = false;
                }
            }
            catch (Exception)
            {

                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Xem lại việc chọn hàng liên kết", loaithongbao.canhbao.GetHashCode());
            }

        }

        private void bton_chon_Click(object sender, EventArgs e)
        {
            mh_select_hanghh frm = new mh_select_hanghh();
            frm.WindowState = FormWindowState.Normal;
            frm.ShowDialog();
            string sMaHang = frm.sMaHang;
            itrangthai = frm.itrangthai;
            this.showInfo(sMaHang);
            bton_luu.Enabled = grid_hh.RowCount > 0;
        }

        //Load danh sach hang hoa
        string m_mahh = "";//
        string m_manhomchu = "";//
        string m_manhomhang = "";//
        int itrangthai = -1;
        void showInfo(string _sMaHang)//20101228
        {
            tv = new thuvien();
            //itrangthai = _itrangthai;// Variable.int_trangthai;
            str_makho = grid_khohang.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho) + "";

            string[] s;
            object[] o;

            if (itrangthai == 0)          // chon nhom chủ
            {
                m_manhomchu = _sMaHang;// Variable.str_manhomchu;
                s = new string[] { "@flag", "@choose", "@manhomchu", "@chu_nhom", "@makho" };
                o = new object[] { 1, 2, m_manhomchu, itrangthai, str_makho };
                tb = tv.SP_SelectDataTable("sp_mh_quanlykho", s, o);
                luoi_hh.DataSource = tb;
            }
            else if (itrangthai == 1)    // chon nhom hang
            {
                m_manhomhang = _sMaHang;// Variable.str_manhomhang;
                s = new string[] { "@flag", "@choose", "@manhomhang", "@chu_nhom", "@makho" };
                o = new object[] { 1, 2, m_manhomhang, itrangthai, str_makho };
                tb = tv.SP_SelectDataTable("sp_mh_quanlykho", s, o);
                luoi_hh.DataSource = tb;
            }
            else if (itrangthai == 2)   // chon hang hoa
            {
                m_mahh = _sMaHang;// m_mahh;
                s = new string[] { "@flag", "@choose", "@mahh", "@chu_nhom" };
                o = new object[] { 1, 2, m_mahh, itrangthai };
                tb = tv.SP_SelectDataTable("sp_mh_quanlykho", s, o);
                luoi_hh.DataSource = tb;
            }
        }
        private void bton_thoat_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mes = new Mod_messagebox();
            //mes.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            //if(Variable.thongbao == true)
            this.Dispose();
        }

        private void bton_toithieu_Click(object sender, EventArgs e)
        {

            txt_toithieu.Text = new cpublic().loadformso_point(670, 270);
            txt_toithieu.Focus();
            if (txt_toithieu.Text == "")
                return;

            //  luu tam những thay đổi của định mức tối thiểu vào bảng
            if (txt_toithieu.Text == "")
                return;

            string makho = grid_khohang.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            string mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
            decimal dinhmuctoithieu = Convert.ToDecimal("0" + txt_toithieu.Text.Replace(",", ""));
            decimal dinhmuctoida = Convert.ToDecimal("0" + txt_toida.Text.Replace(",", ""));

            tb_dinhmuchh.PrimaryKey = new DataColumn[] { tb_dinhmuchh.Columns["MAKHO"],
                                                                                                   tb_dinhmuchh.Columns ["MAHH"] };
            object[] khoa = new object[] { makho, mahh };

            DataView dv = tb_dinhmuchh.DefaultView;
            dv.Sort = "MAKHO,MAHH";
            int chiso = dv.Find(khoa);
            if (chiso > -1)
            {
                tb_dinhmuchh.Rows[chiso]["SOLUONG_MIN"] = txt_toithieu.Text;
                tb_dinhmuchh.Rows[chiso]["SOLUONG_MAX"] = txt_toida.Text;
            }

            else
                tb_dinhmuchh.Rows.Add(new object[] { makho, mahh, dinhmuctoithieu, dinhmuctoida });

            // luu nhung thay đổi định mức tối thiểu vào bảng tb_dinhmuchh_temp và dùng bảng này luu vào csdl
            tb_dinhmuchh_temp.PrimaryKey = new DataColumn[] { tb_dinhmuchh_temp.Columns["MAKHO"],
                                                                                                       tb_dinhmuchh_temp.Columns ["MAHH"] };
            object[] khoa_temp = new object[] { makho, mahh };

            DataView dv_temp = tb_dinhmuchh_temp.DefaultView;
            dv_temp.Sort = "MAKHO,MAHH";
            int chiso_temp = dv_temp.Find(khoa_temp);
            if (chiso_temp > -1)
            {
                tb_dinhmuchh_temp.Rows[chiso_temp]["SOLUONG_MIN"] = txt_toithieu.Text;
                tb_dinhmuchh_temp.Rows[chiso_temp]["SOLUONG_MAX"] = txt_toida.Text;
            }

            else
            {
                tb_dinhmuchh_temp.Rows.Add(new object[] { makho, mahh, dinhmuctoithieu, dinhmuctoida });
            }


            Variable.str_so = "";
        }

        private void bton_nhap_Click(object sender, EventArgs e)
        {
            int iGia = 0;
            int.TryParse(txtGia_NhapXuat.Text.Replace(",", ""), out iGia);
            if (iGia == 0)
            {
                MessageBox_LPV.Show("Nhập giá trước khi nhập số lương");
                return;
            }

            int iSo = 0;
            int.TryParse(txt_nhapxuat.Text, out iSo);

            mh_so frm_so = new mh_so("Nhap Hang Hoa", iSo);
            frm_so.ControlBox = false;
            Variable.int_vitri = Convert.ToInt32(bton_nhap.Tag);
            frm_so.ShowForm();
            if (frm_so.DialogResult != DialogResult.OK) return;

            if (Variable.str_so == "")
                return;
            txt_nhapxuat.Text = Variable.str_so;
            txt_nhapxuat.Focus();
            // nhap kho
            string makho = grid_khohang.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            string mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
            decimal sl_nhap = Convert.ToDecimal(txt_nhapxuat.Text);

            tb_nhapxuatkho.PrimaryKey = new DataColumn[] { tb_nhapxuatkho.Columns["MAKHO"], tb_nhapxuatkho.Columns["MAHH"] };
            object[] khoa = new object[] { makho, mahh };

            DataView dv = tb_nhapxuatkho.DefaultView;
            dv.Sort = "MAKHO,MAHH";
            int chiso = dv.Find(khoa);
            // neu ton tai thi sua
            if (chiso > -1)
            {
                decimal soluong = Convert.ToDecimal(tb_nhapxuatkho.Rows[chiso]["SOLUONG_NHAP"].ToString() == "" ? "0" : tb_nhapxuatkho.Rows[chiso]["SOLUONG_NHAP"].ToString());
                tb_nhapxuatkho.Rows[chiso]["SOLUONG_NHAP"] = soluong + sl_nhap;
                if (tb_nhapxuatkho.Rows[chiso]["SOLUONG_XUAT"].ToString() == "")
                    tb_nhapxuatkho.Rows[chiso]["SOLUONG_XUAT"] = 0;
                tb_nhapxuatkho.Rows[chiso]["GIAMUA"] = iGia;
            }
            else
                tb_nhapxuatkho.Rows.Add(makho, mahh, sl_nhap, 0, iGia, 0);
            Variable.str_so = "";
            decimal tonkhomoi = Convert.ToDecimal(grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_tonkho).ToString()) + Convert.ToDecimal(txt_nhapxuat.Text);
            grid_hh.SetFocusedRowCellValue(col_tonkho, tonkhomoi);
        }

        private void bton_xuat_Click(object sender, EventArgs e)
        {
            #region "  bo "
            //int iGia = 0;
            //int.TryParse(txtGia_NhapXuat.Text.Replace(",", ""), out iGia);
            //if (iGia == 0)
            //{
            //    MessageBox_LPV.Show("Nhập giá trước khi nhập số lương");
            //    return;
            //}

            //int iSo = 0;
            //int.TryParse(txt_nhapxuat.Text, out iSo);
            //mh_so frm_so = new mh_so("Xuat Hang Hoa",iSo);
            //frm_so.ControlBox = false;
            //Variable.int_vitri = Convert.ToInt32(bton_xuat.Tag);
            //frm_so.ShowForm();
            //if (frm_so.DialogResult != DialogResult.OK) return;
            ////frm_so.ShowDialog();
            //if (Variable.str_so == "")
            //    return;

            //if (check_warntoithieu.Checked == true)
            //{
            //    decimal sl_tonkho = Convert.ToDecimal(grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_tonkho).ToString());
            //    decimal sl_dmtt = Convert.ToDecimal(txt_toithieu.Text);
            //    decimal sl_conlai = sl_tonkho - Convert.ToDecimal(Variable.str_so);
            //    if (sl_conlai < sl_dmtt)
            //    {
            //        Mod_messagebox mess = new Mod_messagebox();
            //        mess.messagebox_pos("Thông báo", "Xuất hàng hóa vượt quá định mức tối thiểu", loaithongbao.yesno.GetHashCode());
            //        if (Variable.thongbao == false)
            //            return;
            //    }
            //}

            //txt_nhapxuat.Text = Variable.str_so;
            //txt_nhapxuat.Focus();

            //// xuat kho
            //string makho = grid_khohang.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            //string mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
            //decimal sl_xuat = Convert.ToDecimal(txt_nhapxuat.Text);

            //tb_nhapxuatkho.PrimaryKey = new DataColumn[] { tb_nhapxuatkho.Columns["MAKHO"], tb_nhapxuatkho.Columns["MAHH"] };
            //object[] khoa = new object[] { makho, mahh };

            //DataView dv = tb_nhapxuatkho.DefaultView;
            //dv.Sort = "MAKHO,MAHH";
            //int chiso = dv.Find(khoa);
            //// neu ton tai thi sua
            //if (chiso > -1)
            //{
            //    decimal soluong = Convert.ToDecimal(tb_nhapxuatkho.Rows[chiso]["SOLUONG_XUAT"].ToString() == "" ? "0" : tb_nhapxuatkho.Rows[chiso]["SOLUONG_XUAT"].ToString());
            //    tb_nhapxuatkho.Rows[chiso]["SOLUONG_XUAT"] = soluong + sl_xuat;
            //    if (tb_nhapxuatkho.Rows[chiso]["SOLUONG_NHAP"] + "" == "")
            //        tb_nhapxuatkho.Rows[chiso]["SOLUONG_NHAP"] = 0;
            //    tb_nhapxuatkho.Rows[chiso]["GIABAN"] = iGia;
            //}
            //// neu khong ton tai thi them mới vào bang
            //else
            //    tb_nhapxuatkho.Rows.Add(makho, mahh, 0, sl_xuat,0, iGia);

            //decimal tonkhomoi = Convert.ToDecimal(grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_tonkho).ToString()) - Convert.ToDecimal(txt_nhapxuat.Text);
            //grid_hh.SetFocusedRowCellValue(col_tonkho, tonkhomoi);

            //Variable.str_so = "";
            //txt_nhapxuat.Text = "0";
            #endregion

            frmPHIEU_XUAT frm = new frmPHIEU_XUAT();
            frm.ShowDialog();
        }

        // Chon và chỉnh sửa ten hàng hoa liên ket
        private void bton_edit_ten_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    // lay chi so dong
            //    int index = grid_hhlienket.FocusedRowHandle;
            //    // goi form select _hhlienket
            //    mh_select__hhlienket frm_hhlienket = new mh_select__hhlienket();
            //    frm_hhlienket.StartPosition = FormStartPosition.CenterScreen;
            //    frm_hhlienket.ShowDialog();

            //      tb_hhlienket.PrimaryKey = new DataColumn[] { tb_hhlienket.Columns["MAHHLIENKET"] };
            //    if (tb_hhlienket.Rows.Contains(Variable.str_mahhlienket))  // neu da ton tai mot hang hoa lien ket
            //    {
            //        Mod_messagebox mess = new Mod_messagebox();
            //        mess.messagebox_pos("Thông báo", "Đã tồn tại hàng hóa này", loaithongbao.canhbao.GetHashCode());
            //        return;
            //    }
            //    // neu hang hoa lien ket da ton tai thi insert
            //    if (index >= 0 && index <= luoi_hhlienket.DefaultView.RowCount - 1)     // khi lick vao dong du lieu
            //    {
            //        tb_hhlienket.Rows[index]["TENHHLIENKET"] = Variable.str_tenhh;
            //        tb_hhlienket.Rows[index]["MAHHLIENKET"] = Variable.str_mahhlienket;
            //        tb_hhlienket.Rows[index]["SOLUONG"] = grid_hhlienket.GetRowCellValue(grid_hhlienket.FocusedRowHandle, col_soluong).ToString();
            //        luoi_hhlienket.DataSource = null;
            //        luoi_hhlienket.DataSource = tb_hhlienket;
            //        return;
            //    }
            //    // neu hang hoa lien ket khong ton tai thi insert

            //    m_mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
            //    tb_hhlienket.Rows.Add(m_mahh, Variable.str_mahhlienket, 0, "", Variable.str_tenhh);
            //    luoi_hhlienket.MainView = grid_hhlienket;
            //    luoi_hhlienket.DataSource = tb_hhlienket;
            //    m_mahh = "";
            //    Variable.str_mahhlienket = "";
            //    Variable.str_tenhh = "";

            //}
            //catch (Exception)
            //{

            //    Mod_messagebox mess = new Mod_messagebox();
            //    mess.messagebox_pos("Thông báo","Xem lại việc chon hàng hóa liên kết", loaithongbao.canhbao.GetHashCode());
            //}


        }

        // Chọn và chỉnh sửa số lượng hàng hóa liên kết
        private void bton_edit_soluong_Click(object sender, EventArgs e)
        {

            //try
            //{
            //     // lay chi so dong hien hanh
            //    int index = grid_hhlienket.FocusedRowHandle;
            //    // goi form nhap so
            //    mh_so frm_so = new mh_so();
            //    frm_so.ShowDialog();
            //    // nếu chưa nhập số lượng ở form số thì return 
            //    if (Variable.str_so == "")
            //        return;
            //    // neu hang hoa lien ket da ton tai thi insert
            //    if (index >= 0 && index <= luoi_hhlienket.DefaultView.RowCount - 1)  // khi lick vao dong du lieu
            //    {
            //        // neu chua chon hang hoa lien ket thi return
            //        if (grid_hhlienket.GetRowCellValue(grid_hhlienket.FocusedRowHandle, col_tenhh).ToString() == "")
            //        {
            //            Mod_messagebox mess = new Mod_messagebox();
            //            mess.messagebox_pos("Thông báo", "Chưa chọn hàng hóa liên kết", loaithongbao.canhbao.GetHashCode());
            //            return;
            //        }
            //        // neu da chon hang hoa lien ket
            //        tb_hhlienket.Rows[index]["SOLUONG"] = Variable.str_so;
            //        luoi_hhlienket.DataSource = null;
            //        luoi_hhlienket.DataSource = tb_hhlienket;

            //    }
            //}
            //catch (Exception)
            //{


            //}

        }

        private void grid_hh_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

            if (luoi_hh.DefaultView.DataRowCount > 0)
            {
                check_usekho.Checked = true;
                string smakho = grid_khohang.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
                m_mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
             
                // hien thi hang hoa lien ket
                luoi_hhlienket.DataSource = null;
                luoi_hhlienket.DataSource = f_luoihhlienket(m_mahh);
                tb_hhlienket = f_luoihhlienket(m_mahh);

                // hien thi so luong hang hoa toi thieu
                tb_dinhmuchh.PrimaryKey = new DataColumn[] { tb_dinhmuchh.Columns["MAKHO"], tb_dinhmuchh.Columns ["MAHH"] };
                object[] khoa = new object[] { smakho, m_mahh };

                // MessageBox.Show("ton tai");
                DataView dv = tb_dinhmuchh.DefaultView;
                dv.Sort = "MAKHO,MAHH";
                int chiso = dv.Find(khoa);
                if (chiso > -1)
                {
                    txt_toithieu.Text = tb_dinhmuchh.Rows[chiso]["SOLUONG_MIN"].ToString();
                    txt_toida.Text = tb_dinhmuchh.Rows[chiso]["SOLUONG_MAX"].ToString();
                }
                else
                {
                    txt_toithieu.Text = "0";
                    txt_toida.Text = "0";
                }

                txt_nhapxuat.Text = "0";
                m_mahh = "";
            }
        }

        private void bton_luuhhlienket_Click(object sender, EventArgs e)
        {
            if (luoi_hhlienket.DefaultView.RowCount > 0)
            {
                try
                {
                    for (int i = 0; i < luoi_hhlienket.DefaultView.RowCount - 1; i++)
                    {
                        string mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
                        string mahhlienkeat = grid_hhlienket.GetRowCellDisplayText(i, col_ma);
                        string tenhhlienket = grid_hhlienket.GetRowCellDisplayText(i, col_tenhh);
                        double soluong = Convert.ToDouble(grid_hhlienket.GetRowCellDisplayText(i, col_soluong));
                        // chi insert dong co du lieu
                        if (tenhhlienket != "" && mahhlienkeat != "")
                        {
                            s = new string[] { "@flag", "@choose", "@mahh", "@mahhlienket", "@soluong" };
                            o = new object[] { 2, 1, mahh, mahhlienkeat, soluong };
                            tv = new thuvien();
                            int ketqua = tv.SP_Update("sp_mh_quanlykho", s, o);
                            if (ketqua == 2) // cập nhật không thanh cong
                            {
                                Mod_messagebox mess = new Mod_messagebox();
                                mess.messagebox_pos("Thông báo", tenhhlienket + " không cập nhật được", loaithongbao.canhbao.GetHashCode());
                            }
                        }


                    }

                }
                catch (Exception)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }

            }
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {

            if (luoi_hhlienket.DefaultView.RowCount > 0)
            {
                try
                {

                    string mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
                    string mahhlienkeat = grid_hhlienket.GetRowCellValue(grid_hhlienket.FocusedRowHandle, col_ma).ToString(); //grid_hhlienket.GetRowCellDisplayText(i, col_ma);
                    string tenhhlienket = grid_hhlienket.GetRowCellValue(grid_hhlienket.FocusedRowHandle, col_tenhh).ToString(); //grid_hhlienket.GetRowCellDisplayText(i, col_tenhh);
                    // double soluong = Convert.ToDouble(grid_hhlienket.GetRowCellDisplayText(i, col_soluong));
                    // chi insert dong co du lieu
                    if (tenhhlienket != "" && mahhlienkeat != "")
                    {
                        s = new string[] { "@flag", "@choose", "@mahh", "@mahhlienket" };
                        o = new object[] { 4, 1, mahh, mahhlienkeat };
                        tv = new thuvien();
                        int ketqua = tv.SP_Update("sp_mh_quanlykho", s, o);
                        if (ketqua == 2) // xóa không thanh cong
                        {
                            Mod_messagebox mess = new Mod_messagebox();
                            mess.messagebox_pos("Thông báo", tenhhlienket + " chưa được xóa", loaithongbao.canhbao.GetHashCode());
                            return;
                        }
                        int chiso;
                        chiso = ((ColumnView)luoi_hhlienket.Views[0]).FocusedRowHandle;
                        DataView temp_luoi = (DataView)luoi_hhlienket.Views[0].DataSource;
                        temp_luoi.Delete(chiso);
                    }
                }
                catch (Exception)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }

            }

        }

        private void bton_edit_ten_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                // lay chi so dong
                int index = grid_hhlienket.FocusedRowHandle;
                // goi form select _hhlienket
                mh_select__hhlienket frm_hhlienket = new mh_select__hhlienket();
                frm_hhlienket.StartPosition = FormStartPosition.CenterScreen;
                frm_hhlienket.ShowDialog();
                if (Variable.bool_close == true)
                {
                    return;
                }

                tb_hhlienket.PrimaryKey = new DataColumn[] { tb_hhlienket.Columns["MAHHLIENKET"] };
                if (tb_hhlienket.Rows.Contains(Variable.str_mahhlienket))  // neu da ton tai mot hang hoa lien ket
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Đã tồn tại hàng hóa này", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // neu hang hoa lien ket da ton tai thi update
                if (index >= 0 && index <= luoi_hhlienket.DefaultView.RowCount - 1)     // khi lick vao dong du lieu
                {
                    tb_hhlienket.Rows[index]["TENHHLIENKET"] = Variable.str_tenhh;
                    tb_hhlienket.Rows[index]["MAHHLIENKET"] = Variable.str_mahhlienket;
                    tb_hhlienket.Rows[index]["SOLUONG"] = grid_hhlienket.GetRowCellValue(grid_hhlienket.FocusedRowHandle, col_soluong).ToString();
                    luoi_hhlienket.DataSource = null;
                    luoi_hhlienket.DataSource = tb_hhlienket;
                    return;
                }
                // neu hang hoa lien ket khong ton tai thi insert

                m_mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
                tb_hhlienket.Rows.Add(m_mahh, Variable.str_mahhlienket, 0, "", Variable.str_tenhh);
                luoi_hhlienket.MainView = grid_hhlienket;
                luoi_hhlienket.DataSource = tb_hhlienket;

            }
            catch (Exception)
            {

                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Xem lại việc chon hàng hóa liên kết", loaithongbao.canhbao.GetHashCode());
            }
            finally
            {
                m_mahh = "";
                Variable.str_mahhlienket = "";
                Variable.str_tenhh = "";
                Variable.bool_close = false;
            }
        }

        // Chọn và chỉnh sửa số lượng hàng hóa liên kết
        private void bton_edit_soluong_DoubleClick(object sender, EventArgs e)
        {
            try
            {

                // lay chi so dong hien hanh
                int index = grid_hhlienket.FocusedRowHandle;
                // goi form nhap so
                mh_so frm_so = new mh_so();
                frm_so.ShowDialog();
                // nếu chưa nhập số lượng ở form số thì return 
                if (Variable.str_so == "")
                    return;
                // neu hang hoa lien ket da ton tai thi insert
                if (index >= 0 && index <= luoi_hhlienket.DefaultView.RowCount - 1)  // khi lick vao dong du lieu
                {
                    // neu chua chon hang hoa lien ket thi return
                    if (grid_hhlienket.GetRowCellValue(grid_hhlienket.FocusedRowHandle, col_tenhh).ToString() == "")
                    {
                        Mod_messagebox mess = new Mod_messagebox();
                        mess.messagebox_pos("Thông báo", "Chưa chọn hàng hóa liên kết", loaithongbao.canhbao.GetHashCode());
                        return;
                    }
                    // neu da chon hang hoa lien ket
                    tb_hhlienket.Rows[index]["SOLUONG"] = Variable.str_so;
                    luoi_hhlienket.DataSource = null;
                    luoi_hhlienket.DataSource = tb_hhlienket;

                }
            }
            catch (Exception)
            {


            }

        }

        // luu va dong form
        private void bton_luu_Click(object sender, EventArgs e)
        {
            Int16 ketqua = 0;
            if (check_usekho.Checked == true)
            {
                ketqua = f_luudinhmuctonkho();
                if (ketqua == 0)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Cập nhật hay thêm định mức tồn kho tối thiểu không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                bton_luu.Enabled = false;
                ketqua = f_luuxuatnhapkho();
                if (ketqua == 0)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Cập nhật nhập xuất kho không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }


            ketqua = f_luu_cbhhduoidmtt();
            if (ketqua == 0)
            {
                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Cảnh báo HH dưới định mức tối thiểu không thành công", loaithongbao.canhbao.GetHashCode());
                return;
            }
            ketqua = f_luu_cpbanamkho();
            if (ketqua == 0)
            {
                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Cho phép bán hàng âm kho không thành công", loaithongbao.canhbao.GetHashCode());
                return;
            }
            ketqua = f_luu_cpbhduoidmtt();
            if (ketqua == 0)
            {
                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Cập nhật nhập cho phép bán hàng dưới định mức tối thiểu không thành công", loaithongbao.canhbao.GetHashCode());
                return;
            }
            MessageBox_LPV.Show("Lưu thành công");
            //--------19/07/2012-------------
            #region "Fix refresh form sau khi lưu"
            mh_quanlykho_Load(null, null);
            check_usekho_CheckedChanged(null, null);
            txt_nhapxuat.Text = "0";
            txtGia_NhapXuat.Text = "0";
            #endregion
            //this.Close();
        }

        private void bton_toida_Click(object sender, EventArgs e)
        {

            txt_toida.Text = new cpublic().loadformso_point(670, 270);
            txt_toida.Focus();
            if (txt_toida.Text == "")
                return;
            //  luu tam những thay đổi của định mức tối thiểu vào bảng
            if (txt_toida.Text == "")
                return;
            string makho = "";
            string mahh = "";
            if (grid_khohang.RowCount < 0)
                return;
            if (grid_hh.RowCount < 0)
                return;
            makho = grid_khohang.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            mahh = grid_hh.GetRowCellValue(grid_hh.FocusedRowHandle, col_mahh).ToString();
            decimal dinhmuctoithieu = Convert.ToDecimal("0" + txt_toithieu.Text.Replace(",", ""));
            decimal dinhmuctoida = Convert.ToDecimal("0" + txt_toida.Text.Replace(",", ""));

            tb_dinhmuchh.PrimaryKey = new DataColumn[] { tb_dinhmuchh.Columns["MAKHO"],
                                                                                                   tb_dinhmuchh.Columns ["MAHH"] };
            object[] khoa = new object[] { makho, mahh };

            DataView dv = tb_dinhmuchh.DefaultView;
            dv.Sort = "MAKHO,MAHH";
            int chiso = dv.Find(khoa);
            if (chiso > -1)
            {
                tb_dinhmuchh.Rows[chiso]["SOLUONG_MIN"] = txt_toithieu.Text;
                tb_dinhmuchh.Rows[chiso]["SOLUONG_MAX"] = txt_toida.Text;
            }

            else
                tb_dinhmuchh.Rows.Add(new object[] { makho, mahh, dinhmuctoithieu, dinhmuctoida });

            // luu nhung thay đổi định mức tối thiểu vào bảng tb_dinhmuchh_temp và dùng bảng này luu vào csdl
            tb_dinhmuchh_temp.PrimaryKey = new DataColumn[] { tb_dinhmuchh_temp.Columns["MAKHO"],
                                                                                                       tb_dinhmuchh_temp.Columns ["MAHH"] };
            object[] khoa_temp = new object[] { makho, mahh };

            DataView dv_temp = tb_dinhmuchh_temp.DefaultView;
            dv_temp.Sort = "MAKHO,MAHH";
            int chiso_temp = dv_temp.Find(khoa_temp);
            if (chiso_temp > -1)
            {
                tb_dinhmuchh_temp.Rows[chiso_temp]["SOLUONG_MIN"] = txt_toithieu.Text;
                tb_dinhmuchh_temp.Rows[chiso_temp]["SOLUONG_MAX"] = txt_toida.Text;
            }

            else
            {
                tb_dinhmuchh_temp.Rows.Add(new object[] { makho, mahh, dinhmuctoithieu, dinhmuctoida });

            }



            Variable.str_so = "";
        }

        #endregion

        private void xtraTabControl1_Click(object sender, EventArgs e)
        {

        }

        private void bton_baocao_Click(object sender, EventArgs e)
        {


            if (luoi_khohang.MainView.RowCount <= 0)
            {
                Mod_messagebox mes = new Mod_messagebox();
                mes.messagebox_pos("Thông báo", "Chưa chọn kho hàng", loaithongbao.thongtin.GetHashCode());
                return;
            }
            string khohang = grid_khohang.GetRowCellValue(grid_khohang.FocusedRowHandle, tenkho).ToString();

            DataTable tb = (DataTable)luoi_hh.DataSource;
            if (tb.Rows.Count <= 0)
            {
                Mod_messagebox mes = new Mod_messagebox();
                mes.messagebox_pos("Thông báo", "Không có dữ liệu", loaithongbao.thongtin.GetHashCode());
                return;
            }

            DataSet dset = new DataSet();
            dset.Clear();
            dset.Tables.Add(tb);

            bc_tonkhohientai rpt = new bc_tonkhohientai();
            Mod_tonkhothientai tonkhohientai = new Mod_tonkhothientai();
            tonkhohientai.rpt_baocaotonkhohientai(dset, rpt, khohang);


        }

        private void bton_tonkhobandau_Click(object sender, EventArgs e)
        {
            frmDM_SDBDKHO frm_sdbdkho = new frmDM_SDBDKHO();
            frm_sdbdkho.ShowDialog();
            if (frm_sdbdkho.DialogResult == DialogResult.OK)
                this.showInfo(m_mahh);
        }

        #region "nhap kho nguyen lieu"
        private void bton_them_phieu_nl_Click(object sender, EventArgs e)
        {
            com_nguyenlieu_khohang.Text = "";
            txt_nguyenlieu_sophieu.Text = "";
            date_nguyenlieu_ngaylapphieu.Text = DateTime.Today.ToString("dd/MM/yyyy");
            them_phieu_nguyenlieu = true;
        }

        private void bton_luu_nl_Click(object sender, EventArgs e)
        {
            decimal _tongtien;
            decimal.TryParse(txt_tongtien.Text, out _tongtien);
            if (com_nguyenlieu_khohang.Text == "")
                return;
            mess = new Mod_messagebox();
            int ketqua = -1;

            if (them_phieu_nguyenlieu == true)  // them
            {
                ketqua = f_update_phieunhapnguyenlieu(2, 1, txt_nguyenlieu_sophieu.Text, com_nguyenlieu_khohang._GetselectedID(), new xulyngaythangnam().chuyenngay(date_nguyenlieu_ngaylapphieu.Text), _tongtien);
                if (ketqua > -1)
                {
                    mess.messagebox_pos("Thông báo", "Cập nhật thành công", loaithongbao.thongtin.GetHashCode());
                    luoi_phieunhapnguyenlieu.DataSource = null;
                    luoi_phieunhapnguyenlieu.DataSource = tv.SP_SelectDataTable("sp_kho_nguyenlieu", new string[] { "@flag", "@choose" }, new object[] { 1, 2 });
                    txt_nguyenlieu_sophieu.Text = grid_phieunhapnguyenlieu.GetRowCellValue(grid_phieunhapnguyenlieu.FocusedRowHandle, col_nguyenlieu_sophieu) + "";
                    them_phieu_nguyenlieu_ct = true;
                    bton_nguyenlieu_ten_Click(null, null);
                    txt_phieu_nl_soluong.Text = "0";
                    txt_thanhtien.Text = "0";
                    luoi_phieunhapnguyenlieu_ct.DataSource = null;
                }
                else
                    mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.thongtin.GetHashCode());
            }
            else          // sua
            {
                ketqua = f_update_phieunhapnguyenlieu(3, 1, txt_nguyenlieu_sophieu.Text, com_nguyenlieu_khohang._GetselectedID(), new xulyngaythangnam().chuyenngay(date_nguyenlieu_ngaylapphieu.Text), _tongtien);
                if (ketqua > -1)
                {
                    mess.messagebox_pos("Thông báo", "Cập nhật thành công", loaithongbao.thongtin.GetHashCode());
                    grid_phieunhapnguyenlieu.SetFocusedRowCellValue(col_nguyenlieu_kho, com_nguyenlieu_khohang._GetselectedID());
                    grid_phieunhapnguyenlieu.SetFocusedRowCellValue(col_nguyenlieu_ngaylapphieu, date_nguyenlieu_ngaylapphieu.Text);
                }
                else
                    mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.thongtin.GetHashCode());
            }

        }

        private void grid_phieunhapnguyenlieu_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (grid_phieunhapnguyenlieu.RowCount > 0)
            {
                com_nguyenlieu_khohang._SetselectedID(grid_phieunhapnguyenlieu.GetRowCellValue(grid_phieunhapnguyenlieu.FocusedRowHandle, col_nguyenlieu_kho) + "");
                txt_nguyenlieu_sophieu.Text = grid_phieunhapnguyenlieu.GetRowCellValue(grid_phieunhapnguyenlieu.FocusedRowHandle, col_nguyenlieu_sophieu) + "";
                date_nguyenlieu_ngaylapphieu.Text = grid_phieunhapnguyenlieu.GetRowCellDisplayText(grid_phieunhapnguyenlieu.FocusedRowHandle, col_nguyenlieu_ngaylapphieu) + "";
                txt_tongtien.Text = grid_phieunhapnguyenlieu.GetRowCellDisplayText(grid_phieunhapnguyenlieu.FocusedRowHandle, col_tongtien) + "";
                string maphieunhap = grid_phieunhapnguyenlieu.GetRowCellDisplayText(grid_phieunhapnguyenlieu.FocusedRowHandle, col_nguyenlieu_sophieu) + "";
                luoi_phieunhapnguyenlieu_ct.DataSource = null;
                luoi_phieunhapnguyenlieu_ct.DataSource = tv.SP_SelectDataTable("sp_kho_nguyenlieu",
                                                                                                                              new string[] { "@flag", "@choose", "@id", "@MAKHO" },
                                                                                                                              new object[] { 1, 3, maphieunhap, com_nguyenlieu_khohang._GetselectedID() });

            }
        }

        private void bton_xoa_nl_Click(object sender, EventArgs e)
        {
            int ketqua = -1;
            string str_maphieunhap = grid_phieunhapnguyenlieu.GetRowCellValue(grid_phieunhapnguyenlieu.FocusedRowHandle, col_nguyenlieu_sophieu) + "";

            ketqua = tv.SP_Update("sp_kho_nguyenlieu", new string[] { "@flag", "@choose", "@id" }, new object[] { 4, 1, str_maphieunhap });
            mess = new Mod_messagebox();
            if (ketqua > -1)
            {
                mess.messagebox_pos("Thông báo", "Cập nhật thành công", loaithongbao.thongtin.GetHashCode());
                int chiso = ((ColumnView)luoi_phieunhapnguyenlieu.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_phieunhapnguyenlieu.Views[0].DataSource;
                tam.Delete(chiso);
            }
            else
            {
                mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.thongtin.GetHashCode());
            }
        }

        private void bton_sua_nl_Click(object sender, EventArgs e)
        {
            them_phieu_nguyenlieu = false;
        }

        private void bton_them_ctnl_Click(object sender, EventArgs e)
        {
            them_phieu_nguyenlieu_ct = true;
            txt_nguyenlieu_ten.Text = "";
            txt_phieu_nl_soluong.Text = "";
            txt_nguyenlieu_toida.Text = "";
            txt_nguyenlieu_toithieu.Text = "";
            bton_nguyenlieu_ten.Enabled = true;
        }

        private void bton_thoat_nguyenlieu_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bton_sua_ctnl_Click(object sender, EventArgs e)
        {
            them_phieu_nguyenlieu_ct = false;
            bton_nguyenlieu_ten.Enabled = false;
            float.TryParse(txt_phieu_nl_soluong.Text, out m_soluongbandau);
        }

        private void bton_nguyenlieu_ten_Click(object sender, EventArgs e)
        {
            mh_select_nguyenlieu frm_select_nguyenlieu = new mh_select_nguyenlieu();
            frm_select_nguyenlieu.ShowDialog();
            if (frm_select_nguyenlieu.DialogResult == DialogResult.OK)
            {
                str_tennguyenlieu = frm_select_nguyenlieu.str_tennguyenlieu;
                str_manguyenlieu = int.Parse(frm_select_nguyenlieu.str_manguyenlieu.Replace(".", ""));
                str_tendvt = frm_select_nguyenlieu.str_tendvt;
                str_madvt = frm_select_nguyenlieu.str_madvt;

                txt_nguyenlieu_ten.Text = frm_select_nguyenlieu.str_tennguyenlieu;
                lbel_dvt.Text = str_tendvt;

                DataTable tb = tv.SP_SelectDataTable("sp_kho_nguyenlieu",
                                                                            new string[] { "@flag", "@choose", "@MAKHO", "@manguyenlieu" },
                                                                            new object[] { 1, 5, com_nguyenlieu_khohang._GetselectedID(), frm_select_nguyenlieu.str_manguyenlieu });
                if (tb.Rows.Count > 0)
                {
                    txt_nguyenlieu_toithieu.Text = tb.Rows[0]["TOITHIEU"] + "";
                    txt_nguyenlieu_toida.Text = tb.Rows[0]["TOIDA"] + "";
                }

            }

        }

        private void bton_dvt_Click(object sender, EventArgs e)
        {
            string str_soluong = new cpublic().loadformso_point(670, 270);
            if (str_soluong != "")
                txt_phieu_nl_soluong.Text = str_soluong;

        }

        private void bton_luu_ctnl_Click(object sender, EventArgs e)
        {
            //float _tonkhohientai = new thuvien().SP_SelectDataTable("fnc_tonkhoNGUYEN_LIEU", 
            //                                                                                        new[] { "@ma_khohang", "@MA_NGUYEN_LIEU", "@thang_nam" }, 
            //                                                                                        new object[] { });
            int chiso;
            decimal _thanhtien;
            decimal.TryParse(txt_thanhtien.Text, out _thanhtien);
            int ketqua = -1;
            if (txt_nguyenlieu_ten.Text == "" && txt_phieu_nl_soluong.Text == "" && txt_nguyenlieu_sophieu.Text == "")
                return;
            mess = new Mod_messagebox();
            string str_maphieunhap = grid_phieunhapnguyenlieu.GetRowCellValue(grid_phieunhapnguyenlieu.FocusedRowHandle, col_nguyenlieu_sophieu) + "";
            string str_makhonguyenlieu = grid_phieunhapnguyenlieu.GetRowCellValue(grid_phieunhapnguyenlieu.FocusedRowHandle, col_nguyenlieu_kho) + "";

            float float_soluong;
            float.TryParse(txt_phieu_nl_soluong.Text, out float_soluong);
            float _tonmax;
            float.TryParse(txt_nguyenlieu_toida.Text, out _tonmax);
            float _tonmin;
            float.TryParse(txt_nguyenlieu_toithieu.Text, out _tonmin);

            if (them_phieu_nguyenlieu_ct == true)     // them chi tiet phieu nhap nguyen lieu
            {

                // ton kho hien tai
                float _tonkhohientai = new thuvien().SP_SelectReturnOne("fnc_tonkhoNGUYEN_LIEU",
                                                                       new[] { "@ma_khohang", "@MA_NGUYEN_LIEU", "@thang_nam" },
                                                                       new object[] { str_makhonguyenlieu, str_manguyenlieu, DateTime.Today });

                float _tongtonsau = _tonkhohientai + float_soluong;

                if (_tongtonsau > _tonmax && _tonmax > 0)
                {
                    mess.messagebox_pos("Thông báo", "Tồn kho sẽ vượt quá định mức đa?", loaithongbao.yesno.GetHashCode());
                    if (Variable.thongbao == false)
                    {
                        return;
                    }
                }

                ketqua = tv.SP_Update("sp_kho_nguyenlieu",
                     new string[] { "@flag", "@choose", "@id", "@manguyenlieu", "@MAKHO", "@soluong", "@dvt", "@toida", "@toithieu", "@thanhtien" },
                     new object[] { 2, 2, str_maphieunhap, str_manguyenlieu, str_makhonguyenlieu, float_soluong, this.str_madvt, txt_nguyenlieu_toida.Text, txt_nguyenlieu_toithieu.Text, _thanhtien });
                if (ketqua > -1)
                {
                    mess.messagebox_pos("Thông báo", "Cập nhật thành công", loaithongbao.thongtin.GetHashCode());
                    luoi_phieunhapnguyenlieu_ct.DataSource = tv.SP_SelectDataTable("sp_kho_nguyenlieu",
                                                                       new string[] { "@flag", "@choose", "@id", "@MAKHO" },
                                                                       new object[] { 1, 3, str_maphieunhap, str_makhonguyenlieu });

                }
                else
                    mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.thongtin.GetHashCode());

            }
            else                            // sua chi tiet phieu nhap nguyen lieu
            {
                str_maphieunhap = grid_phieunhapnguyenlieu_ct.GetRowCellValue(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_maphieunhapnguyenlieu) + "";
                str_manguyenlieu = int.Parse(grid_phieunhapnguyenlieu_ct.GetRowCellValue(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_manguyenlieu) + "");
                chiso = ((ColumnView)luoi_phieunhapnguyenlieu_ct.Views[0]).FocusedRowHandle;

                // ton kho hien tai
                float _tonkhohientai = new thuvien().SP_SelectReturnOne("fnc_tonkhoNGUYEN_LIEU",
                                                                                                   new[] { "@ma_khohang", "@MA_NGUYEN_LIEU", "@thang_nam" },
                                                                                                   new object[] { str_makhonguyenlieu, str_manguyenlieu, DateTime.Today });

                float _tongtonsau = _tonkhohientai - m_soluongbandau + float_soluong;

                if (_tongtonsau > _tonmax && _tonmax > 0)
                {
                    mess.messagebox_pos("Thông báo", "Tồn kho sẽ vượt quá định mức đa?", loaithongbao.yesno.GetHashCode());
                    if (Variable.thongbao == false)
                    {
                        txt_nguyenlieu_ten.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_ten_nguyenlieu);
                        txt_phieu_nl_soluong.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_soluong_nguyenlieu);
                        txt_nguyenlieu_toida.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_nl_dinhmuctoida);
                        txt_nguyenlieu_toithieu.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_nl_toithieu);
                        lbel_dvt.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_nl_tendvt);
                        return;
                    }

                }

                ketqua = tv.SP_Update("sp_kho_nguyenlieu",
                                                     new string[] { "@flag", "@choose", "@id", "@manguyenlieu", "@MAKHO", "@soluong", "@dvt", "@toida", "@toithieu", "@thanhtien" },
                                                       new object[] { 3, 2, str_maphieunhap, str_manguyenlieu, str_makhonguyenlieu, float_soluong, this.str_madvt, txt_nguyenlieu_toida.Text, txt_nguyenlieu_toithieu.Text, _thanhtien });
                //  new object[] { 3, 2, str_maphieunhap, str_manguyenlieu, float_soluong, txt_nguyenlieu_toida.Text, txt_nguyenlieu_toithieu.Text });

                if (ketqua > -1)
                {
                    mess.messagebox_pos("Thông báo", "Cập nhật thành công", loaithongbao.thongtin.GetHashCode());
                    luoi_phieunhapnguyenlieu_ct.DataSource = null;
                    luoi_phieunhapnguyenlieu_ct.DataSource = tv.SP_SelectDataTable("sp_kho_nguyenlieu",
                                                                                   new string[] { "@flag", "@choose", "@id", "@MAKHO" },
                                                                                   new object[] { 1, 3, str_maphieunhap, str_makhonguyenlieu });
                    grid_phieunhapnguyenlieu_ct.FocusedRowHandle = chiso;
                }
                else
                    mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.thongtin.GetHashCode());

            }
            chiso = grid_phieunhapnguyenlieu.FocusedRowHandle;
            luoi_phieunhapnguyenlieu.DataSource = null;
            luoi_phieunhapnguyenlieu.DataSource = tv.SP_SelectDataTable("sp_kho_nguyenlieu", new string[] { "@flag", "@choose" }, new object[] { 1, 2 });
            txt_nguyenlieu_sophieu.Text = grid_phieunhapnguyenlieu.GetRowCellValue(grid_phieunhapnguyenlieu.FocusedRowHandle, col_nguyenlieu_sophieu) + "";
            txt_tongtien.Text = grid_phieunhapnguyenlieu.GetRowCellValue(grid_phieunhapnguyenlieu.FocusedRowHandle, col_tongtien) + "";
            grid_phieunhapnguyenlieu.FocusedRowHandle = chiso;

        }

        private void luoi_phieunhapnguyenlieu_DataSourceChanged(object sender, EventArgs e)
        {
            if (grid_phieunhapnguyenlieu.RowCount > 0)
                group_chitietphieu.Enabled = true;
            else
                group_chitietphieu.Enabled = false;
        }

        private void grid_phieunhapnguyenlieu_ct_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (grid_phieunhapnguyenlieu_ct.RowCount > 0)
            {
                txt_nguyenlieu_ten.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_ten_nguyenlieu);
                txt_phieu_nl_soluong.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_soluong_nguyenlieu);
                txt_nguyenlieu_toida.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_nl_dinhmuctoida);
                txt_nguyenlieu_toithieu.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_nl_toithieu);
                lbel_dvt.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_nl_tendvt);
                txt_thanhtien.Text = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_thanhtien);
            }

        }

        private void bton_xoa_ctnl_Click(object sender, EventArgs e)
        {
            if (grid_phieunhapnguyenlieu_ct.RowCount > 0)
            {
                mess = new Mod_messagebox();
                string maphieunhap_nl = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_maphieunhapnguyenlieu);
                string manguyenlieu = grid_phieunhapnguyenlieu_ct.GetRowCellDisplayText(grid_phieunhapnguyenlieu_ct.FocusedRowHandle, col_manguyenlieu);

                int ketqua = -1;
                ketqua = tv.SP_Update("sp_kho_nguyenlieu",
                                                    new string[] { "@flag", "@choose", "@id", "@manguyenlieu" },
                                                    new object[] { 4, 2, maphieunhap_nl, manguyenlieu });
                if (ketqua > -1)
                {
                    mess.messagebox_pos("Thông báo", "Cập nhật thành công", loaithongbao.thongtin.GetHashCode());
                    int chiso = ((ColumnView)luoi_phieunhapnguyenlieu_ct.Views[0]).FocusedRowHandle;
                    DataView tam = (DataView)luoi_phieunhapnguyenlieu_ct.Views[0].DataSource;
                    tam.Delete(chiso);

                }
                else
                    mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.thongtin.GetHashCode());

            }
        }

        private void bton_toida_nl_Click(object sender, EventArgs e)
        {
            string toida_nl = new cpublic().loadformso_point(670, 270);
            if (toida_nl != "")
                txt_nguyenlieu_toida.Text = toida_nl;
        }

        private void bton_toithieu_nl_Click(object sender, EventArgs e)
        {
            string toithieu_nl = new cpublic().loadformso_point(670, 270);
            if (toithieu_nl != "")
                txt_nguyenlieu_toithieu.Text = toithieu_nl;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (txt_barcode.Text != "")
            {
                string _barcode = txt_barcode.Text;
                txt_barcode.Text = "";
                txt_barcode_2.Text = _barcode;
                f_showhh_barcode(_barcode);
            }
        }

        private void bton_use_barcode_Click(object sender, EventArgs e)
        {
            txt_barcode.Focus();
        }

        private void bton_barcode_Click(object sender, EventArgs e)
        {
            string _barcode = new cpublic().loadformchu_point(300, 250);
            if (_barcode != "")
            {
                f_showhh_barcode(_barcode);
            }
        }

        private void btnGia_Nhap_Click(object sender, EventArgs e)
        {
            int iGia = 0;
            int.TryParse(txtGia_NhapXuat.Text.Replace(",",""), out iGia);

            mh_so frm_so = new mh_so("Giá", iGia);
            iGia = frm_so.ShowForm();
            if (frm_so.DialogResult == DialogResult.OK)
                txtGia_NhapXuat.Text = iGia + "";
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string str_sotien = new cpublic().loadformso_point(670, 270);
            if (str_sotien != "")
                txt_thanhtien.Text = str_sotien;
        }

        private void btnDS_Phieu_Nhap_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            string sfile = Application.StartupPath + "\\LPV.PhieuNhapHH.exe";
            if (System.IO.File.Exists(sfile))
                System.Diagnostics.Process.Start(sfile);
            this.Cursor = Cursors.Default;
        }

        private void btnDS_Phieu_Nhap_NVL_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            string sfile = Application.StartupPath + "\\LPV.PhieuNhapHH.exe";
            if (System.IO.File.Exists(sfile))
                System.Diagnostics.Process.Start(sfile);
            this.Cursor = Cursors.Default;
        }
        #endregion

        private void btnCapNhatNVL_TrongThang_Click(object sender, EventArgs e)
        {
            frmPHIEU_TONKHO_NGUYEN_VAT_LIEU frm = new frmPHIEU_TONKHO_NGUYEN_VAT_LIEU();
            frm.ShowDialog();
        }

    }

       

}
